﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace Kang.WPF.Controls.Pager
{
    /// <summary>
    /// Pager.xaml 的交互逻辑
    /// </summary>
    public partial class Pager : UserControl
    {
        public Pager()
        {
            InitializeComponent();
            DataContext = this;
        }

        public Visibility PageIndexVisibility
        {
            get { return (Visibility)GetValue(PageIndexVisibilityProperty); }
            set { SetValue(PageIndexVisibilityProperty, value); }
        }

        public static readonly DependencyProperty PageIndexVisibilityProperty =
            DependencyProperty.Register("PageIndexVisibility", typeof(Visibility), typeof(Pager), new PropertyMetadata(Visibility.Visible));

        public Visibility PageCountVisibility
        {
            get { return (Visibility)GetValue(PageCountVisibilityProperty); }
            set { SetValue(PageCountVisibilityProperty, value); }
        }

        public static readonly DependencyProperty PageCountVisibilityProperty =
            DependencyProperty.Register("PageCountVisibility", typeof(Visibility), typeof(Pager), new PropertyMetadata(Visibility.Visible));

        public Visibility PageNumSelectVisibility
        {
            get { return (Visibility)GetValue(PageNumSelectVisibilityProperty); }
            set { SetValue(PageNumSelectVisibilityProperty, value); }
        }

        public static readonly DependencyProperty PageNumSelectVisibilityProperty =
            DependencyProperty.Register("PageNumSelectVisibility", typeof(Visibility), typeof(Pager), new PropertyMetadata(Visibility.Visible));

        public Visibility DataCountVisibility
        {
            get { return (Visibility)GetValue(DataCountVisibilityProperty); }
            set { SetValue(DataCountVisibilityProperty, value); }
        }

        public static readonly DependencyProperty DataCountVisibilityProperty =
            DependencyProperty.Register("DataCountVisibility", typeof(Visibility), typeof(Pager), new PropertyMetadata(Visibility.Visible));

        public Visibility JumpVisibility
        {
            get { return (Visibility)GetValue(JumpVisibilityProperty); }
            set { SetValue(JumpVisibilityProperty, value); }
        }

        public static readonly DependencyProperty JumpVisibilityProperty =
            DependencyProperty.Register("JumpVisibility", typeof(Visibility), typeof(Pager), new PropertyMetadata(Visibility.Visible));

        public int PageIndex
        {
            get { return (int)GetValue(PageIndexProperty); }
            set { SetValue(PageIndexProperty, value); }
        }

        public static readonly DependencyProperty PageIndexProperty =
            DependencyProperty.Register("PageIndex", typeof(int), typeof(Pager), new PropertyMetadata(0));

        public int PageSize
        {
            get { return (int)GetValue(PageSizeProperty); }
            //set { SetValue(PageSizeProperty, value); }
        }

        public static readonly DependencyProperty PageSizeProperty =
            DependencyProperty.Register("PageSize", typeof(int), typeof(Pager), new PropertyMetadata(50));

        public int PageCount
        {
            get { return (int)GetValue(PageCountProperty); }
            set { SetValue(PageCountProperty, value); }
        }

        public static readonly DependencyProperty PageCountProperty =
            DependencyProperty.Register("PageCount", typeof(int), typeof(Pager), new PropertyMetadata(0));

        public int DataCount
        {
            get { return (int)GetValue(DataCountProperty); }
            set
            {
                SetValue(DataCountProperty, value);
                if (value > 0)
                {
                    SetValue(DataCountProperty, value);
                }
                else
                {
                    SetValue(DataCountProperty, 0);
                }
            }
        }

        public static readonly DependencyProperty DataCountProperty =
            DependencyProperty.Register("DataCount", typeof(int), typeof(Pager), new PropertyMetadata(0));

        public string JumpNum
        {
            get
            {
                return (string)GetValue(JumpNumProperty);
            }
            set
            {
                SetValue(JumpNumProperty, value);
            }
        }

        public static readonly DependencyProperty JumpNumProperty =
            DependencyProperty.Register("JumpNum", typeof(string), typeof(Pager), null);

        public Brush ButtonBackground
        {
            get { return (Brush)GetValue(ButtonBackgroundProperty); }
            set { SetValue(ButtonBackgroundProperty, value); }
        }

        public static readonly DependencyProperty ButtonBackgroundProperty =
            DependencyProperty.Register("ButtonBackground", typeof(Brush), typeof(Pager), new PropertyMetadata((Brush)(new BrushConverter()).ConvertFromString("#337AB7")));

        public Brush ButtonTextForeground
        {
            get { return (Brush)GetValue(ButtonTextForegroundProperty); }
            set { SetValue(ButtonTextForegroundProperty, value); }
        }

        public static readonly DependencyProperty ButtonTextForegroundProperty =
            DependencyProperty.Register("ButtonTextForeground", typeof(Brush), typeof(Pager), new PropertyMetadata(Brushes.White));

        public new Brush BorderBrush
        {
            get { return (Brush)GetValue(BorderBrushProperty); }
            set { SetValue(BorderBrushProperty, value); }
        }

        public static readonly new DependencyProperty BorderBrushProperty =
            DependencyProperty.Register("BorderBrush", typeof(Brush), typeof(Pager));

        public new Brush Background
        {
            get { return (Brush)GetValue(BackgroundProperty); }
            set { SetValue(BackgroundProperty, value); }
        }

        public static readonly new DependencyProperty BackgroundProperty =
            DependencyProperty.Register("Background", typeof(Brush), typeof(Pager));



        public new Thickness BorderThickness
        {
            get { return (Thickness)GetValue(BorderThicknessProperty); }
            set { SetValue(BorderThicknessProperty, value); }
        }

        public static readonly new DependencyProperty BorderThicknessProperty =
            DependencyProperty.Register("BorderThickness", typeof(Thickness), typeof(Pager));


        public float CornerRadius
        {
            get { return (float)GetValue(CornerRadiusProperty); }
            set { SetValue(CornerRadiusProperty, value); }
        }

        public static readonly DependencyProperty CornerRadiusProperty =
            DependencyProperty.Register("CornerRadius", typeof(float), typeof(Pager));

        public event RoutedEventHandler NextPageButtonOnClick
        {
            add
            {
                this.AddHandler(NextPageButtonOnClickEvent, value);
            }
            remove { this.RemoveHandler(NextPageButtonOnClickEvent, value); }
        }
        public static readonly RoutedEvent NextPageButtonOnClickEvent =
            EventManager.RegisterRoutedEvent("NextButtonOnClick", RoutingStrategy.Bubble, typeof(RoutedEventArgs), typeof(Pager));

        private void btn_next_Click(object sender, RoutedEventArgs e)
        {
            PageButtonEventArgs args = new PageButtonEventArgs(NextPageButtonOnClickEvent, this);
            if (PageCount < 0)
            {
                PageCount = 0;
            }
            if (PageIndex < PageCount)
            {
                PageIndex++;
            }
            else
            {
                PageIndex = PageCount;
            }
            args.PageIndex = PageIndex;
            RaiseEvent(args);
            GC.Collect();
        }

        public event RoutedEventHandler PreviousPageButtonOnClick
        {
            add
            {
                this.AddHandler(PreviousPageButtonOnClickEvent, value);
            }
            remove { this.RemoveHandler(PreviousPageButtonOnClickEvent, value); }
        }
        public static readonly RoutedEvent PreviousPageButtonOnClickEvent =
            EventManager.RegisterRoutedEvent("PreviousButtonOnClick", RoutingStrategy.Bubble, typeof(RoutedEventArgs), typeof(Pager));

        private void btn_previous_Click(object sender, RoutedEventArgs e)
        {
            PageButtonEventArgs args = new PageButtonEventArgs(PreviousPageButtonOnClickEvent, this);
            if (PageIndex > 0)
            {
                PageIndex--;
            }
            args.PageIndex = PageIndex;
            RaiseEvent(args);
            GC.Collect();
        }

        public event RoutedEventHandler GoPageButtonOnClick
        {
            add
            {
                this.AddHandler(GoPageButtonOnClickEvent, value);
            }
            remove { this.RemoveHandler(GoPageButtonOnClickEvent, value); }
        }
        public static readonly RoutedEvent GoPageButtonOnClickEvent =
            EventManager.RegisterRoutedEvent("GoButtonOnClick", RoutingStrategy.Bubble, typeof(RoutedEventArgs), typeof(Pager));

        private void btn_go_Click(object sender, RoutedEventArgs e)
        {
            PageButtonEventArgs args = new PageButtonEventArgs(GoPageButtonOnClickEvent, this);
            int inputPageIndex = 0;
            int.TryParse(JumpNum, out inputPageIndex);
            if (PageCount < 0)
            {
                PageCount = 0;
            }
            if (inputPageIndex <= PageCount && inputPageIndex > 0)
            {
                PageIndex = inputPageIndex;
            }
            args.PageIndex = PageIndex;
            RaiseEvent(args);
            GC.Collect();
        }

        public event RoutedEventHandler RefreshPageButtonOnClick
        {
            add
            {
                this.AddHandler(RefreshPageButtonOnClickEvent, value);
            }
            remove { this.RemoveHandler(RefreshPageButtonOnClickEvent, value); }
        }
        public static readonly RoutedEvent RefreshPageButtonOnClickEvent =
            EventManager.RegisterRoutedEvent("RefreshButtonOnClick", RoutingStrategy.Bubble, typeof(RoutedEventArgs), typeof(Pager));

        private void btn_refresh_Click(object sender, RoutedEventArgs e)
        {
            PageButtonEventArgs args = new PageButtonEventArgs(RefreshPageButtonOnClickEvent, this);
            args.PageIndex = PageIndex;
            RaiseEvent(args);
            GC.Collect();
        }

        private void cmb_pagesizeset_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            var cbi = e.AddedItems[0] as ComboBoxItem;
            if (cbi != null)
            {
                int size = Int32.Parse(cbi.Content as String);
                SetValue(PageSizeProperty, size);
                PageButtonEventArgs args = new PageButtonEventArgs(RefreshPageButtonOnClickEvent, this);
                args.PageIndex = PageIndex;
                RaiseEvent(args);
                GC.Collect();
            }
        }
    }
}
